#!/usr/bin/fish

mkdir -p (pwd)/download
mkdir -p /usr/local/bin{frp,shadowsocks}
mkdir -p /etc/{shadowsocks,frp,caddy}
mkdir -p (pwd)/download
mkdir -p /var/www

set -x TITLE "haku zero"

set -x LC_ALL C

set -x DOWNLOAD_DIR (pwd)/download

set -x NETWORK_IP (curl ifconfig.me -s)

set -x REPO_URL https://gitee.com/jjjjjjjh/sys-config/raw/master/systemd

if test (uname -m | grep 64)
  set -x ARCH amd64
else
  set -x ARCH i386
end

function log
    if test $argv
        printf $argv"\n"
    end
end

function purple
    set_color purple
    log $argv
    set_color normal
end

function error
    set_color red
    log $argv
    set_color normal
end

function green
    set_color green
    log $argv
    set_color normal
end

function blue
    set_color blue
    log $argv
    set_color normal
end

function update
    sudo apt update ;and sudo apt upgrade -y
    install_soft
end

function install_soft
    sudo apt install -y vim git-core zsh tmux curl python-pip htop ncdu wget
end

function printInfo
    green "Master: "(who am i | awk '{print $1}')
    green "HOSTNAME: "(hostname)
    green "CPU: "(cat /proc/cpuinfo | grep name |sed -n '1p'| awk -F ':' '{print $2}')
    green "RAM: "(grep MemTotal /proc/meminfo | awk '{print $2}')" Kb"
    green "Inner Ip: "(ip addr show | grep inet | grep 192.168 | awk '{print $2}')
    if test $NETWORK_IP
        green "Network Ip: $NETWORK_IP"
    else
        error "Don't have network"
        exit 1
    end
end

function init
    purple $TITLE
    printInfo
    blue "\nsystem initalization...."
    update
end

function install_go
    wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
    sudo tar -C /usr/local -xzf go1.9.2.linux-amd64.tar.gz
end

function install_nodejs
    curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
    sudo apt install -y nodejs
end

function install_docker
    sudo apt install -y linux-image-extra-(uname -r) linux-image-extra-virtual;and sudo apt update
    sudo apt install -y apt-transport-https ca-certificates software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo apt-key fingerprint 0EBFCD88
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu "(lsb_release -cs)" stable"
    sudo apt update
    sudo apt install -y docker-ce
end

function install_docker-compose
    sudo pip install -U docker-compose
end

function install_caddy
    curl https://getcaddy.com | bash -s personal
    read -l address -p 'echo "enter caddy ip or http name:"'
    if test -n !$address
        set address 127.0.0.1
    end
    echo $address"{
        root /var/www
    }"> /etc/caddy/Caddyfile
    sudo wget -p /etc/systemd/system $REPO_URL"/caddy.service"
    set -c address
end

function install_shadowsocks
    sudo pip install git+https://github.com/shadowsocks/shadowsocks.git@master
    read -l port -p 'echo "enter shadowsock port:"'
    read -l passwd -p 'echo "enter shadowsock passwd:"'
    echo "
        {
            \"server\":\""$NETWORK_IP"\",
            \"server_port\":"$port",
            \"password\":\""$passwd"\",
            \"timeout\":300,
            \"method\":\"aes-256-cfb\",
            \"fast_open\": false
        }
    " | sed 's/ //g' > /etc/shadowsocks/ss_config
    set -e port
    set -e passwd
    sudo wget -p /etc/systemd/system $REPO_URL"/ssserver.service"
end

function install_frp
    # 获取frp最后一个版本
    set -x frp_release curl https://api.github.com/repos/fatedier/frp/releases/latest -s | grep tag_name | awk -F '"' '{print $4}' | sed 's/v//g'
    wget -p $DOWNLOAD_DIR "https://github.com/fatedier/frp/releases/download/v"$frp_release"/frp_"$frp_release"_linux_"$ARCH".tar.gz" -o frp.tar.gz
    tar xvzf $DOWNLOAD_DIR"/frp" -C $DOWNLOAD_DIR"/frp"
    sudo cp $DOWNLOAD_DIR"/frp/fprs" /usr/bin
    echo "[common]
        bind_port = 7000
        dashboard_port = 7500
        dashboard_user = admin
        dashboard_pwd = admin
        kcp_bind_port = 7000
    " | sed 's/ //g' > /etc/frp/frps.ini
    bule "frp installed version: "$frp_release
    rm -rf $DOWNLOAD_DIR"/frp"
    sudo wget -p /etc/systemd/system $REPO_URL"/frp.service"
    set -e frp_release
end

function systemd_reload
    sudo systemctl daemon-reload
    sudo systemctl enable caddy.service
    sudo systemctl enable frp.service
    sudo systemctl enable ssserver.service
    blue "now print system status..."
    sudo systemctl status caddy.service
    sudo systemctl status frp.service
    sudo systemctl status ssserver.service
end

function install
    green "install golang..."
    install_go
    green "install nodejs..."
    install_nodejs
    green "install docker..."
    install_docker
    green "install docker-compose..."
    install_docker-compose

    install_frp
    install_caddy
    install_shadowsocks
    green "systemd reload..."
    systemd_reload
end

init

install